package com.iplant.wms.serviceImpl.dao.brm;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.brm.BRMDistributionRule;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@DBBaseDAO(BRMDistributionRule.class)
public class BRMDistributionRuleDAO extends BaseDAO {

    private static BRMDistributionRuleDAO Instance = null;

    private BRMDistributionRuleDAO() {
        super("");
        this.Init();
    }

    public static BRMDistributionRuleDAO getInstance() {
        if (Instance == null) {
            Instance = new BRMDistributionRuleDAO();
        }
        return Instance;
    }


    public int BRM_DeleteDistributionRule(
            BMSEmployee wLoginUser, BRMDistributionRule wDistributionRule, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        int wResult = 0;
        try {

            wResult = this.DeletePO(wLoginUser, wDistributionRule, wErrorCode);

            if (wErrorCode.Result != 0) {
                return wResult;
            }
            if (wResult > 0) {
                BRMDistributionPropDAO.getInstance().BRM_DeleteDistributionProp(
                        wLoginUser, wDistributionRule.ID, wErrorCode);
                BRMDistributionRangeDAO.getInstance().BRM_DeleteDistributionRange(
                        wLoginUser, wDistributionRule.ID, wErrorCode);
            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("BRM_DeleteDistributionRule", ex);
        }
        return wResult;
    }


    public List<BRMDistributionRule> BRM_QueryDistributionRuleList(
            BMSEmployee wLoginUser, int wMethod, int wFirstType, String wName, int wActive, Pagination wPagination,
            OutResult<Integer> wErrorCode) {

        Map<String, Object> wParams = new HashMap<>();

        wParams.put("Method", wMethod);
        wParams.put("FirstType", wFirstType);
        wParams.put("Name", wName);

        wParams.put("Active", wActive);


        return this.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);
    }


}
